package bkone;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/admin_login")
public class admin_login_servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	public static List<Map<String,String>> remoteUserInfos = new ArrayList<>();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public admin_login_servlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//单个指定参数获取
		String nameStr = request.getParameter("name");
		String passwordStr = request.getParameter("password");
	
		String webResult = "";
		if ("gly".equals(nameStr) && "glykl".equals(passwordStr)) {
			webResult = "登录成功！";
			System.out.println("登录成功！");
			
			
			Map<String,String> userInfoMap = new HashMap<>();
			userInfoMap.put("bwinfo", request.getHeader("User-Agent"));
			userInfoMap.put("ip", request.getRemoteAddr());
			userInfoMap.put("sessionid", request.getRequestedSessionId());
			remoteUserInfos.add(userInfoMap);
			
			//登录session设置
			HttpSession session = request.getSession();
			session.setAttribute("ip", request.getRemoteAddr());
			session.setAttribute("user", nameStr);
			
			response.sendRedirect("/admin_main.html");
		} else {
			webResult = "登录失败！用户名或密码错误！";
			System.out.println("登录失败！用户名或密码错误！");
		}

		response.setCharacterEncoding("utf-8");
		response.addCookie(new Cookie("userid", "test"));
		
		
		response.getWriter().append("登录结果: " + webResult);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}